package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"travel-app/api/client"
	"travel-app/api/model"
	"travel-app/common/proto/travel"
)

func EsSync(c *gin.Context) {
	sync, err := client.EsSync(c, &travel.EsSyncReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ES同步成功",
		"data": sync,
	})
}

func EsSyncGet(c *gin.Context) {
	var data model.ES
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	get, err := client.EsSyncGet(c, &travel.EsSyncGetReq{Keyword: data.Keyword})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "es查询成功",
		"data": get,
	})
	return
}

func AreaList(c *gin.Context) {
	var data model.Area
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	get, err := client.AreaList(c, &travel.AreaListReq{Name: data.Name})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "景点详情查询成功",
		"data": get,
	})
}

func BuyTicKet(c *gin.Context) {
	var data model.BuyTicKet
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	id := c.GetUint("userId")

	get, err := client.BuyTicKet(c, &travel.BuyTicKetReq{
		AreaId:       data.AreaId,
		Sum:          data.Sum,
		Days:         data.Days,
		TicketStatus: data.TicketStatus,
		UserId:       int64(id),
		//Insurance:    data.Insurance,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户购买门票成功",
		"data": get,
	})
}

func CallBack(c *gin.Context) {
	fmt.Println("收到支付回调")

	OrderSn := c.Request.FormValue("out_trade_no")
	status := c.Request.FormValue("trade_status")

	if status == "TRADE_SUCCESS" {
		status = "1"
	}

	atoi, _ := strconv.Atoi(status)

	back, err := client.CallBack(c, &travel.PayCallbackReq{
		OrderSn: OrderSn,
		Status:  int64(atoi),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "支付宝回调成功",
		"data": back.Success,
	})

}

func OrderList(c *gin.Context) {
	var data model.OrderID
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	get, err := client.OrderList(c, &travel.FindOrderReq{UserID: data.UserId})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查看订单成功",
		"data": get,
	})
}

func DelOrder(c *gin.Context) {
	var data model.DelOrder
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	get, err := client.OrderDel(c, &travel.DelOrderReq{
		OrderSn: data.OrderSn,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单退款成功",
		"data": get,
	})
}
